﻿using DbFactory;
using PlaneClean.Models;
using PlaneClean.Service;
using PlaneCleanEntity;
using PlaneCleanEntity.ViewEntity.RouterEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Http;

namespace PlaneClean.Controllers.ManagerController
{
    /// <summary>
    /// 路由控制器
    /// </summary>
    public class RouterController  : ManagerController
    {
        /// <summary>
        /// 根据登录用户角色权限查询菜单列表树 路由
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public ListResponse<RouterVo> List(BaseRequest request)
        {
            ListResponse<RouterVo> response = new ListResponse<RouterVo>();
            response.bodyList = new List<RouterVo>();
            response.result = "NG";

            List<t_menu> menuList = new List<t_menu>();
            List<RouterVo> rmList = new List<RouterVo>();
            List<int> menuIdList = new List<int>();
            List<int> roleIdList = new List<int>();
            t_user user = dbFactory.Db.Queryable<t_user>().Select("*").First(a => a.u_token == request.token);
            if (user == null)
            {
                response.reason = "没有找到对应token的用户,请重新登录";
                return response;
            }
            List<t_user_group> userGroupList = dbFactory.Db.Queryable<t_user_group>()
               .Select("*").Where(a => a.u_id == user.u_id).ToList();
            foreach (var ug in userGroupList)
            {
                t_group group = new t_group();
                Role role = new Role();
                group = dbFactory.Db.Queryable<t_group>().First(a => a.g_id == ug.g_id);
                if (group != null)
                {
                    roleIdList.Add(group.g_id);
                }
            }
            if (roleIdList != null || roleIdList.Count > 0)
            {
                menuIdList = dbFactory.Db.Queryable<t_group_menu>().Where(a => roleIdList.Contains(a.g_id)).Select(a => a.m_id).ToList();
                menuList = dbFactory.Db.Queryable<t_menu>().Where(a => menuIdList.Contains(a.m_id)).Where(a => a.m_state == 1).OrderBy(a=>a.m_sequence).ToList();

            }
            else
            {
                menuList = dbFactory.Db.Queryable<t_menu>().Where(a => a.m_state == 1).OrderBy(a => a.m_sequence).ToList();
            }
            if (menuList != null && menuList.Count > 0)
            {
                foreach (var menu in menuList)
                {
                    RouterVo rm = new RouterVo();
                    rm.alwaysShow = true;
                    rm.component = "Layout";
                    if (menu.m_parent_id > 0)
                    {
                        rm.component = menuList.Where(a => a.m_id == menu.m_parent_id).FirstOrDefault<t_menu>().m_url;
                    }
                    rm.hidden = false;
                    rm.id = menu.m_id;
                    rm.parentId = menu.m_parent_id ?? 0;
                    rm.meta = new RouterMetaVo();
                    rm.meta.activeMenu = "";
                    rm.meta.affix = false;
                    rm.meta.breadcrumb = false;
                    rm.meta.icon = menu.m_icon;
                    rm.meta.keepAlive = false;
               
                    if (menu.m_cache == 1)
                    {
                        rm.meta.keepAlive = true;
                    }
                    rm.meta.path = menu.m_url;
                    rm.meta.noCache = false;
              
                    rm.meta.roles = new List<string>();

                    if (!string.IsNullOrEmpty(menu.m_buttens))
                    {
                        List<string> buttensList = dbFactory.Db.Queryable<t_group_menu>()
                            .Where(a => roleIdList.Contains(a.g_id) && a.m_id == menu.m_id).Select(a => a.m_buttens).ToList();
                        string buttens = string.Join(",",buttensList);
                        buttensList = buttens.Split(',').ToList();
                        buttensList = buttensList.Where((x, i) => buttensList.FindIndex(z => z == x) == i).ToList();
                        buttensList.Remove("");
                        List<string> buttensNew = new List<string>();
                        if (buttensList.Contains("Add"))
                        {
                            buttensNew.Add("Add");
                        }
                        if (buttensList.Contains("Update"))
                        {
                            buttensNew.Add("Update");
                        }
                        if (buttensList.Contains("Delete"))
                        {
                            buttensNew.Add("Delete");
                        }
                        if (buttensList.Contains("Export"))
                        {
                            buttensNew.Add("Export");
                        }
                        rm.meta.roles = buttensNew;
                  
                        //rm.meta.roles = menu.m_buttens.Split(',').ToList();
                    }

                    rm.meta.title = menu.m_name;

                    rm.name = menu.m_name;
                    rm.path = menu.m_url;
                    rm.redirect = menu.m_redirect;
                    rmList.Add(rm);
                }
            }
            MenuSerivce ms = new MenuSerivce();
            rmList = ms.ToTreeMenu(rmList, 0);
            response.bodyList.AddRange(rmList);
            response.result = "OK";
            return response;
        }
    }
}